package com.infore.common.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 水质等级计算工具
 *
 * @author cys
 */
public class WaterQualityUtil {

    public static final int invalidLevel = 999999;

    /**
     * 水质等级
     */
    private static final Map<String, Integer> LEVEL = new HashMap<String, Integer>() {
        private static final long serialVersionUID = 1L;

        {
            put("default", 0);
            put("one", 1);
            put("two", 2);
            put("three", 3);
            put("four", 4);
            put("five", 5);
            put("vfive", 6);
        }
    };

    /**
     * 因子等级标准
     */
    private static final Map<String, List<Double>> projectWaterQuality = new HashMap<String, List<Double>>() {
        private static final long serialVersionUID = 12L;

        {
            put("w01009", Arrays.asList(7.5, 6.0, 5.0, 3.0, 2.0));          //溶解氧 //大于
            put("w21003", Arrays.asList(0.15, 0.5, 1.0, 1.5, 2.0));         //氨氮
            put("w21011", Arrays.asList(0.02, 0.1, 0.2, 0.3, 0.4));         //总磷
            put("w21001", Arrays.asList(0.2, 0.5, 1.0, 1.5, 2.0));          //总氮
            put("w01019", Arrays.asList(2.0, 4.0, 6.0, 10.0, 15.0));        //高锰酸盐指数
            put("w01018", Arrays.asList(15.0, 15.0, 20.0, 30.0, 40.0));     //cod
            put("w01001", Arrays.asList(6.0, 9.0));                         //ph
            put("w01003", Arrays.asList(-99999.0, 99999.0));                //浊度
            put("w01014", Arrays.asList(-99999.0, 99999.0));                //电导率
            put("w01010", Arrays.asList(-99999.0, 99999.0));                //温度
            put("w01023", Arrays.asList(-99999.0, 99999.0));                //生物毒性
        }
    };

    public static String waterQualityEvaluation(Integer level) {
        if (level == 1 || level == 2) {
            return "优";
        } else if (level == 3) {
            return "良好";
        } else if (level == 4) {
            return "轻度污染";
        } else if (level == 5) {
            return "中度污染";
        } else if (level == 6) {
            return "重度污染";
        } else {
            return "-";
        }
    }


    public static Integer getWaterLevel(String code, String value) {
        if (code == null || value == null) return 3;

        if (code.equals("w01023")) {
            return 3;
        }

        List<Double> levelList = projectWaterQuality.get(code);
        if (levelList == null) {
            return 3;
        }
        double val = Double.valueOf(value);
        if (code.equals("w01009")) {
            for (int i = 0; i < levelList.size(); i++) {
                if (val >= levelList.get(i)) {
                    return i + 1;
                }
            }
        } else if (code.equals("w01001")) {
            if (val >= 6 && val <= 9) {
                return 1;
            }
        } else {
            for (int i = 0; i < levelList.size(); i++) {
                if (val <= levelList.get(i)) {
                    return i + 1;
                }
            }
        }
        return LEVEL.get("vfive");

    }


}
